package com.baizan.daka.ui.activity

import android.content.Context
import android.content.Intent
import android.view.View
import com.baizan.base.BaseActivity
import com.baizan.daka.mvp.model.bean.BankCardInfoBean
import com.baizan.daka.showToast
import com.baizan.daka.ui.contract.AddBankContract
import com.baizan.daka.ui.presenter.AddBankPresenter
import com.baizan.daka.util.IcCradUtil
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_add_bankcard.*

class AddBankCardActivity : BaseActivity<AddBankPresenter>(), AddBankContract.View {
    var bankInfo:BankCardInfoBean? = null
    companion object {
        fun start(context: Context, bankCard: BankCardInfoBean? = null) {
            val intent = Intent(context, AddBankCardActivity::class.java)
            bankCard?.let {
                intent.putExtra("Bank", it)
            }
            context.startActivity(intent)
        }
    }

    override fun initPresenter(): AddBankPresenter = AddBankPresenter(this)

    override fun layoutId(): Int = R.layout.act_add_bankcard

    override fun initData() {
        val bank = intent.getSerializableExtra("Bank")
        bank?.let {
            it as BankCardInfoBean
            bankInfo = it
            name_et.setText(it.bankUser)
            card_name_et.setText(it.cardNo)
            bank_name_et.setText(it.bankName)
            kaihuhang_value.setText(it.bankBranch)
            submit_bt.text="确定"
            name_et.setSelection(it.bankUser.length)
        }?:let {
            submit_bt.text="添加"
        }
    }

    override fun initView() {
        bank_name_et.setOnFocusChangeListener(object : View.OnFocusChangeListener {
            override fun onFocusChange(v: View?, hasFocus: Boolean) {
                if (hasFocus) {
                    if (bank_name_et.text.isNullOrEmpty()) {
                        val bankName = IcCradUtil.getNameOfBank(card_name_et.text.toString())
                        bank_name_et.setText(bankName)
                    }
                }
            }
        })
        submit_bt.setOnClickListener {
            var name = name_et.text
            if (name.isEmpty()) {
                showToast("请输入持卡人")
                return@setOnClickListener
            }
            var cardId = card_name_et.text
            if (cardId.isEmpty()) {
                showToast("请输入银行卡号")
                return@setOnClickListener
            }
            var bankName = bank_name_et.text
            if (bankName.isEmpty()) {
                showToast("请输入银行名称")
                return@setOnClickListener
            }
            var kaihuhangValue = kaihuhang_value.text
            if (kaihuhangValue.isEmpty()) {
                showToast("请输入开户行")
                return@setOnClickListener
            }

            bankInfo?.let {
                presenter?.modifyBank(it.id,
                    cardId.toString(), bankName.toString(),
                    kaihuhangValue.toString(), name.toString()
                )
            } ?:let {
                presenter?.addBank(
                    cardId.toString(), bankName.toString(),
                    kaihuhangValue.toString(), name.toString()
                )
            }
        }
    }

    override fun start() {
    }

    override fun addBankSuccess() {
        showToast("绑定成功")
        finish()
    }

    override fun modifySuccess() {
        showToast("修改成功")
        finish()
    }

}